package com.tusuo.stds.ui.scene;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.ModuleList;
import com.tusuo.stds.rest.SceneList;
import com.tusuo.stds.ui.module.ModuleActivity_;
import com.tusuo.stds.ui.scenedetail.SceneDetailActivity_;
import com.tusuo.stds.util.MyClassicsHeader;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.parceler.Parcels;

import java.util.List;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;
import timber.log.Timber;

/**
 * Created by developer on 17-8-30.
 */
@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(SceneFragment.class)
@EFragment(R.layout.frag_scene)
public class SceneFragment extends Fragment {

    @FragmentArg("scene")
    SceneList.Scene mScene;
    @FragmentArg("moduleList")
    List<ModuleList.Module> Data;

    @ViewById(R.id.frag_scene_tv_no_modules)
    TextView tvNoModules;

    @ViewById(R.id.frag_scene_description)
    TextView tvDescription;
    @ViewById(R.id.frag_scene_list_module)
    ListView listView;

    @ViewById(R.id.frag_scene_layout_detail)
    RelativeLayout layoutDetail;

    @ViewById(R.id.frag_scene_refresh_header)
    MyClassicsHeader header;

    @Inject
    ApiHelper mApiHelper;

    public static final String FROM_SELF = "from_self";
    private boolean firstLoad = true;
    private ModuleListAdapter adapter;

    @AfterInject
    void initDI() {
        Injector.initSceneFragmentComponent().inject(this);
    }

    @AfterViews
    void init() {
        tvDescription.setText(mScene.Description);

        tvNoModules.setVisibility(View.GONE);
        header.setOnHeaderScrollListener(new MyClassicsHeader.OnHeaderScrollListener() {
            @Override
            public boolean onHeaderScroll(float percent) {
                if (percent > 0.8f) {
                    handler.sendEmptyMessageDelayed(888,500);
                    return true;
                }
                return false;
            }
        });

        initData();

        firstLoad = false;
    }

    private void initData() {
        if (Data == null || Data.size() < 1) {
            showNoModulesTips();
        }else {
            adapter = new ModuleListAdapter(getActivity(), Data);
            listView.setAdapter(adapter);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!firstLoad) {
            initData();
            mApiHelper.getModuleList(new GetModuleListSubscriber(),mScene.Id);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
//        if (!hidden && !firstLoad) {
//            initData();
//            mApiHelper.getModuleList(new GetModuleListSubscriber(),mScene.Id);
//        }
    }

    void jumpToDetail() {
        Intent intent = new Intent(getActivity(), SceneDetailActivity_.class);
        intent.putExtra(SceneList.Scene.EXTRA_NAME, Parcels.wrap(mScene));
        intent.putExtra(FROM_SELF, true);
        startActivity(intent);
    }

    @ItemClick(R.id.frag_scene_list_module)
    void listItemClicked(ModuleList.Module module) {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        Intent intent = new Intent();
        intent.setClass(getActivity(), ModuleActivity_.class);
        intent.putExtra(ModuleList.Module.EXTRA_NAME, Parcels.wrap(module));
        startActivity(intent);
    }

    public void showNoModulesTips() {
        tvNoModules.setVisibility(View.VISIBLE);
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 888) {
                jumpToDetail();
            }
        }
    };


    private class GetModuleListSubscriber extends Subscriber<ModuleList> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
        }

        @Override
        public void onNext(final ModuleList list) {
            if (list.Total > 0) {
                Data = list.Data;
                initData();
            }
        }
    }

}
